package com.sc.nft.controller;

import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.UserPushBindCidRequest;
import com.sc.nft.controller.request.UserPushConfRequest;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.UserPushConfVo;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.UserPushConfService;
import com.sc.nft.service.UserRelationPushCidService;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * @author 刘国航
 * @since 2024/6/4 11:15
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/userPush")
@Api(tags = "用户推送")
public class UserPushController {

    private final UserRelationPushCidService userRelationPushCidService;
    private final UserPushConfService userPushConfService;
    private final Redisson redisson;

    @ApiOperation(value = "绑定客户端cid", notes = "绑定客户端cid", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/bindCid", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result bindCid(@Valid @RequestBody UserPushBindCidRequest request)  {
        UserInfo user = UserPool.getUser();
        RLock lock = redisson.getLock(RedisKeyEnum.PUSH_BIND_CID.getKey() + user.getId());
        try {
            if (!lock.tryLock(10, 10, TimeUnit.SECONDS)) {
                throw new GlobalException("登录频繁，请稍后重试");
            }
            userRelationPushCidService.bindCid(request.getCid(), request.getPlatform(), user);
        } catch (Exception e) {
            log.error("绑定客户端cid异常:", e);
            return Result.fail(e.getMessage());
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
        return Result.success();
    }


    @ApiOperation(value = "获取消息管理配置", notes = "获取消息管理配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/detailConf", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<UserPushConfVo> detailConf()  {
        return Result.success(userPushConfService.detailConf(UserPool.getUser()));
    }

    @ApiOperation(value = "设置消息管理配置", notes = "设置消息管理配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateConf", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result updateConf(@Valid @RequestBody UserPushConfRequest request)  {
        UserInfo user = UserPool.getUser();
        RLock lock = redisson.getLock(RedisKeyEnum.PUSH_UPDATE_CONF.getKey() + user.getId());
        try {
            if (!lock.tryLock(10, 10, TimeUnit.SECONDS)) {
                throw new GlobalException("操作频繁，请稍后重试");
            }
            userPushConfService.updateConf(request.getEnableMallOrderPush(), request.getEnablePledgeTaskPush(), request.getEnableNoticePush(), user);
        } catch (Exception e) {
            log.error("设置消息管理配置异常:", e);
            return Result.fail(e.getMessage());
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
        return Result.success();
    }

}
